package com.cheer.ba.view.fragment;

import android.Manifest;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.EntityCache;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.H5Address;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.model.ArticleModel;
import com.cheer.ba.model.BannerModel;
import com.cheer.ba.model.BreakRulesModel;
import com.cheer.ba.model.HotExchangeModel;
import com.cheer.ba.model.OilPriceModel;
import com.cheer.ba.model.PlayerModel;
import com.cheer.ba.model.PointModel;
import com.cheer.ba.presenter.impl.HomePresenter;
import com.cheer.ba.presenter.impl.PlayerPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.DensityUtils;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.SystemUtils;
import com.cheer.ba.view.activity.BaiduMapAty;
import com.cheer.ba.view.activity.BreakRuleCarListAty;
import com.cheer.ba.view.activity.BreakRulesCarAty;
import com.cheer.ba.view.activity.LookMyBillAty;
import com.cheer.ba.view.activity.CommH5Aty;
import com.cheer.ba.view.activity.CommH5WithTitleAty;
import com.cheer.ba.view.activity.FindOilAty;
import com.cheer.ba.view.activity.FreePlayAty;
import com.cheer.ba.view.activity.MyOilCardAty;
import com.cheer.ba.view.activity.MyPointAty;
import com.cheer.ba.view.activity.OilSetMealAty;
import com.cheer.ba.view.adapter.BannerPagerAdapter;
import com.cheer.ba.view.adapter.HomeChannelAdp;
import com.cheer.ba.view.adapter.HomeChannelHolder;
import com.cheer.ba.view.adapter.HomeOilPriceAdp;
import com.cheer.ba.view.adapter.HotExchangeAdp;
import com.cheer.ba.view.adapter.HotExchangeHolder;
import com.cheer.ba.view.base.BaseCommFrg;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.MarqueeView;
import com.cheer.ba.widget.MyListView;
import com.cheer.ba.widget.banner.BannerIndicator;
import com.cheer.ba.widget.banner.LoopViewPager;
import com.cheer.ba.widget.myrecycleview.BaseAdapter;
import com.cheer.ba.widget.myrecycleview.ScrollRecycler;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页
 */
public class HomeFrg extends BaseCommFrg implements AdapterView.OnItemClickListener {

    //banner
    @BindView(R.id.id_layout_banner)
    FrameLayout mLayoutBanner;
    //轮播图
    @BindView(R.id.id_home_viewpager)
    LoopViewPager mViewpager;
    //轮播图上的点
    @BindView(R.id.id_home_indicator)
    BannerIndicator mIndicator;
    //轮播图适配器
    private BannerPagerAdapter mBannerAdp;
    //记录banner加载失败次数
    private int mRecordBannerCount = 0;
    //得到banner
    private List<BannerModel> mSaveBanners = new ArrayList<BannerModel>();

    //栏目
    @BindView(R.id.id_home_recycler)
    ScrollRecycler mRecycler;
    private HomeChannelAdp mRecyclerAdp;
    private List<HotExchangeModel> mRecyclers = new ArrayList<HotExchangeModel>();

    //热门兑换 -- 积分
    @BindView(R.id.id_hot_exchange_recycler)
    ScrollRecycler mHotExchangeRecycler;
    private HotExchangeAdp mHotExchangeAdp;
    private List<HotExchangeModel> mHotRecyclers = new ArrayList<HotExchangeModel>();

    //热门活动
    @BindView(R.id.id_hot_player_listv)
    MyListView mHotPlayListV;
    private CommonAdapter<PlayerModel> mHotPlayAdp;
    private List<PlayerModel> mHotPlays = new ArrayList<PlayerModel>();

    private PlayerPresenter mPlayerPresenter;

    //今日油讯
    @BindView(R.id.id_today_article_layout)
    LinearLayout mLinearArticle;

    @BindView(R.id.id_today_news_txt)
    MarqueeView mTxtDayNews;
    //存储资讯标题
    private List<String> mSaveName = new ArrayList<String>();
    //存储资讯跳转链接
    private List<String> mSaveUrl = new ArrayList<String>();

    //今日油价
    @BindView(R.id.id_today_oil_price_linear)
    LinearLayout mLinearOilPrice;
    //油价城市
    @BindView(R.id.id_location_price_txt)
    TextView mTxtLocation;
    //油价
    @BindView(R.id.id_oil_price_recycler)
    ScrollRecycler mOilRecycler;
    private HomeOilPriceAdp mOilPriceAdp;
    private List<OilPriceModel> mOilRecyclers = new ArrayList<OilPriceModel>();

    //我的订单总额
    @BindView(R.id.id_order_num_txt)
    TextView mTxtTotalOrder;
    //我的积分总额
    @BindView(R.id.id_point_num_txt)
    TextView mTxtTotalPoint;

    //屏幕的宽度
    private int width;
    //活动图宽度
    private int activity_height;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.frg_home, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new HomePresenter();
        mPlayerPresenter = new PlayerPresenter();
        mPlayerPresenter.attachView(this);
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        width = SystemUtils.getScreenWidth(frg) - DensityUtils.dip2px(frg, 10);
        activity_height = (int) (202 * width / 710);
        mLayoutBanner.getLayoutParams().height = DensityUtils.getScreenW(getActivity()) * 344 / 750;
        mLayoutBanner.requestLayout();

        //栏目
        LinearLayoutManager layoutManager = new LinearLayoutManager(frg);
        //设置布局管理器
        mRecycler.setLayoutManager(layoutManager);
        //设置为水平布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.HORIZONTAL);
//        mRecycler.addItemDecoration(new DividerItemDecoration(frg, DividerItemDecoration.HORIZONTAL_LIST));
        mRecyclerAdp = new HomeChannelAdp(mRecyclers, new OnRecyclerItemClick());
        //设置Adapter
        mRecycler.setAdapter(mRecyclerAdp);
        //设置增加或删除条目的动画
        mRecycler.setItemAnimator(new DefaultItemAnimator());

        //热门兑换
        LinearLayoutManager hotExchange = new LinearLayoutManager(frg);
        mHotExchangeRecycler.setLayoutManager(hotExchange);
        hotExchange.setOrientation(OrientationHelper.HORIZONTAL);
        mHotExchangeAdp = new HotExchangeAdp(mHotRecyclers, new OnRecyclerItemClick1());
        //设置Adapter
        mHotExchangeRecycler.setAdapter(mHotExchangeAdp);
        //设置增加或删除条目的动画
        mHotExchangeRecycler.setItemAnimator(new DefaultItemAnimator());

        //油价
        LinearLayoutManager oilPrice = new LinearLayoutManager(frg);
        mOilRecycler.setLayoutManager(oilPrice);
        oilPrice.setOrientation(OrientationHelper.HORIZONTAL);
        mOilPriceAdp = new HomeOilPriceAdp(mOilRecyclers, null);
        //设置Adapter
        mOilRecycler.setAdapter(mOilPriceAdp);
        //设置增加或删除条目的动画
        mOilRecycler.setItemAnimator(new DefaultItemAnimator());

        //热门活动
        mHotPlayAdp = new CommonAdapter<PlayerModel>(frg, mHotPlays, R.layout.adp_players) {
            @Override
            public void convert(ViewHolder helper, PlayerModel item) {
                if (null != item) {
                    ImageView img = (ImageView) helper.getViewById(R.id.id_players_img);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, activity_height);
                    img.setLayoutParams(params);
                    helper.setRoundImageByUrl(R.id.id_players_img, Protect.setPic(item.getBackImg()), R.drawable.ic_error_show_bg, frg);
                    helper.setText(R.id.id_game_content_txt, item.getLotteryName());
                    //活动是否截至
                    ImageView mImgEnd = (ImageView) helper.getViewById(R.id.id_game_img);
                    mImgEnd.setVisibility(View.VISIBLE);
                    //当前日期
                    long currentTime = System.currentTimeMillis();
                    if (currentTime >= item.getStartDate() && currentTime < item.getEndDate()) {
                        mImgEnd.setVisibility(View.GONE);
                    } else if (currentTime < item.getStartDate()) {
                        //活动未开始
                        mImgEnd.setBackgroundResource(R.drawable.ic_play_no_begin);
                    } else {
                        //活动已结束
                        mImgEnd.setBackgroundResource(R.drawable.ic_game_over);
                    }
                }
            }
        };
        mHotPlayListV.setAdapter(mHotPlayAdp);
        mHotPlayListV.setOnItemClickListener(this);

        mTxtDayNews.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
            @Override
            public void onItemClick(int position, TextView textView) {
                if (null != mSaveUrl && mSaveUrl.size() > position) {
                    CommH5Aty.startCommonH5(frg, mSaveUrl.get(position), true);
                }
            }
        });
    }

    @Override
    public void onNetChange(int netMobile) {
        if (isNetConnect(netMobile)) {
            loadData();
        } else {
            loadDataCache();
        }
    }


    /**
     * 加载首页banner缓存
     */
    public void delazyBannerCache() {
        List<BannerModel> banners = ((HomePresenter) presenter).getCacheData();
        if (null != banners && !banners.isEmpty()) {
            updateViewWithTag(banners, ConstantTag.HOME_BANNER);
        } else {
            mLayoutBanner.setVisibility(View.GONE);
        }
    }

    /**
     * 加载首页热门活动缓存
     */
    public void delazyHotPlayCache() {
        List<PlayerModel> plays = mPlayerPresenter.getCacheData();
        if (null != plays && !plays.isEmpty()) {
            updateViewWithTag(plays, ConstantTag.OIL_PLAYERS);
        } else {
            mHotPlayListV.setVisibility(View.GONE);
        }
    }

    /**
     * 加载今日油价缓存
     */
    public void delazyOilPriceCache() {
        List<HotExchangeModel> oils = ((HomePresenter) presenter).getTodayOilCache();
        if (null != oils && !oils.isEmpty()) {
            updateViewWithTag(oils, ConstantTag.HOME_OIL);
        } else {
            mLinearOilPrice.setVisibility(View.GONE);
        }
    }

    /**
     * 加载今日油讯缓存
     */
    public void delazyArticlesCache() {
        List<ArticleModel> articles = ((HomePresenter) presenter).getTodayArticleCache();
        if (null != articles && !articles.isEmpty()) {
            updateViewWithLoadMore(articles, false, ConstantTag.GET_ARTICLE_LIST);
        } else {
            mLinearArticle.setVisibility(View.GONE);
        }
    }

    public void loadData() {
        //获取banner
        ((HomePresenter) presenter).getBanner();
        //获得活动
        mPlayerPresenter.getPlayers(false);
        //获取今日油价
        ((HomePresenter) presenter).getTodayOilPrice("湖北");
        //获取今日油讯
        ((HomePresenter) presenter).getTodayArticles(false, "1");
        if (Session.getUserIsLogin()) {
            //获取我的消费总金额
            ((HomePresenter) presenter).getMyTotalOrder("", "");
            //获取我的总积分
            ((HomePresenter) presenter).getMyTotalPoint();
        } else {
            mTxtTotalOrder.setText("- -");
            mTxtTotalPoint.setText("- -");
        }
    }

    /**
     * 加载缓存数据
     */
    public void loadDataCache() {
        if (mSaveBanners.isEmpty()) {
            delazyBannerCache();
        }
        if (mHotPlays.isEmpty()) {
            delazyHotPlayCache();
        }
        if (mOilRecyclers.isEmpty()) {
            delazyOilPriceCache();
        }
        if (mSaveName.isEmpty()) {
            delazyArticlesCache();
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        //获得首页标题
        List<HotExchangeModel> recycles = (List<HotExchangeModel>) ((HomePresenter) presenter).getData(ConstantTag.HOME_TITLE);
        mRecyclerAdp.fillList(recycles);

        //获得热门兑换
        List<HotExchangeModel> mHotExchanges = (List<HotExchangeModel>) ((HomePresenter) presenter).getData(ConstantTag.HOME_HOT_EXCHANGE);
        updateViewWithTag(mHotExchanges, ConstantTag.HOME_HOT_EXCHANGE);

        //获得热门活动
//        List<HotPlayModel> mPlays = (List<HotPlayModel>) ((HomePresenter) presenter).getData(ConstantTag.HOME_HOT_PLAY);
//        updateViewWithTag(mPlays, ConstantTag.HOME_HOT_PLAY);

        mTxtLocation.setText("湖北");
        if (inspectNet()) {
            loadData();
        } else {
            loadDataCache();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        if (TextUtils.equals(event.type, IntentKey.Login.KEY_LOGIN_SUCCESS)) {
            //登录成功
            //获取我的消费总金额
            ((HomePresenter) presenter).getMyTotalOrder("", "");
            //获取我的总积分
            ((HomePresenter) presenter).getMyTotalPoint();
        } else if (TextUtils.equals(event.type, IntentKey.Login.KEY_APP_EXIT)) {
            //退出
            mTxtTotalOrder.setText("- -");
            mTxtTotalPoint.setText("- -");
        } else if (TextUtils.equals(event.type, IntentKey.Login.KEY_NOTICE_PAY)) {
            LogUtils.LogD(TAG, "支付成功-----------HomeFrg");
            //支付成功
            //获取我的消费总金额
            ((HomePresenter) presenter).getMyTotalOrder("", "");
        } else if (TextUtils.equals(event.type, IntentKey.Login.KEY_NOTICE_POINT)) {
            LogUtils.LogD(TAG, "积分变更-----------HomeFrg");
            //签到成功
            //获取我的总积分
            ((HomePresenter) presenter).getMyTotalPoint();
        }
    }

    @OnClick({R.id.id_diesel_oil_linear, R.id.id_send_oil_linear, R.id.id_hot_exchange_txt, R.id.id_hot_player_txt, R.id.id_go_my_order_linear, R.id.id_go_my_point_linear})
    public void onClick(View view) {
        switch (view.getId()) {
            //柴油套餐
            case R.id.id_diesel_oil_linear:
                showActivity(frg, OilSetMealAty.class);
                break;
            //柴油批发
            case R.id.id_send_oil_linear:
//                showActivity(frg, SendOilDoorAty.class);
//                    showActivity(frg, AskOilCardAty.class);
//                showActivity(frg, SelectConditionAty.class);
                showActivity(frg, FindOilAty.class);
                break;
            //热门兑换
            case R.id.id_hot_exchange_txt:
                SVProgressHUD.showInfoWithStatus(frg, "热门兑换");
                break;
            //热门活动
            case R.id.id_hot_player_txt:
                showActivity(frg, FreePlayAty.class);
                break;
            case R.id.id_go_my_order_linear:
                //我的订单
                if (isLogin(true)) {
                    showActivity(frg, MyOilCardAty.class);
                }
                break;
            case R.id.id_go_my_point_linear:
                //我的积分
                if (isLogin(true)) {
                    showActivity(frg, MyPointAty.class);
                }
                break;
        }
    }

    /**
     * 数据加载成功
     */
    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(ConstantTag.HOME_BANNER, tag)) {
                mSaveBanners.clear();
                //首页banner
                List<BannerModel> mBanners = (List<BannerModel>) t;
                if (null != mBanners && !mBanners.isEmpty()) {
                    mLayoutBanner.setVisibility(View.VISIBLE);
                    mSaveBanners.addAll(mBanners);
//                if (mBanners.size() > 8) {
//                    mBanners = mBanners.subList(0, 7);
//                }
                    showBannerPager(mBanners);
                }
            } else if (TextUtils.equals(ConstantTag.HOME_HOT_EXCHANGE, tag)) {
                //热门兑换
                List<HotExchangeModel> mHotExchanges = (List<HotExchangeModel>) t;
                if (null != mHotExchanges && !mHotExchanges.isEmpty()) {
                    mHotExchangeAdp.fillList(mHotExchanges);
                }
            } else if (TextUtils.equals(ConstantTag.HOME_HOT_PLAY, tag)) {
                //热门活动
                List<PlayerModel> mPlays = (List<PlayerModel>) t;
                if (null != mPlays && !mPlays.isEmpty()) {
                    mHotPlays.clear();
                    mHotPlays.addAll(mPlays);
                    mHotPlayAdp.notifyDataSetChanged();
                }
            } else if (TextUtils.equals(tag, ConstantTag.OIL_PLAYERS)) {
                //运营活动
                mHotPlayListV.setVisibility(View.VISIBLE);
                List<PlayerModel> plays = (List<PlayerModel>) t;
                mHotPlays.clear();
                mHotPlays.addAll(plays);
                mHotPlayAdp.notifyDataSetChanged();
            } else if (TextUtils.equals(tag, ConstantTag.DATA_ERROR)) {
                //活动数据加载失败
                if (mHotPlays.isEmpty()) {
                    delazyHotPlayCache();
                }
            } else if (TextUtils.equals(tag, ConstantTag.DATA_SUCCESS_NULL)) {
                //没有运营活动
                mHotPlayListV.setVisibility(View.GONE);
            } else if (TextUtils.equals(tag, ConstantTag.HOME_OIL)) {
                //获取今日油价
                mOilRecyclers.clear();
                List<HotExchangeModel> oils = (List<HotExchangeModel>) t;
                if (null != oils && !oils.isEmpty()) {
                    mLinearOilPrice.setVisibility(View.VISIBLE);
                    HotExchangeModel model = oils.get(0);
                    OilPriceModel oil1 = new OilPriceModel("柴油0#", model.p0);
                    oil1.setRed(true);
                    OilPriceModel oil2 = new OilPriceModel("汽油92#", model.p92);
                    OilPriceModel oil3 = new OilPriceModel("汽油95#", model.p95);
                    OilPriceModel oil4 = new OilPriceModel("汽油93#", model.p93);
                    OilPriceModel oil5 = new OilPriceModel("汽油97#", model.p97);
                    OilPriceModel oil6 = new OilPriceModel("汽油90#", model.p90);
                    mOilRecyclers.add(oil1);
                    mOilRecyclers.add(oil2);
                    mOilRecyclers.add(oil3);
                    mOilRecyclers.add(oil4);
                    mOilRecyclers.add(oil5);
                    mOilRecyclers.add(oil6);
//                    mOilPriceAdp.fillList(mOilRecyclers);
                    mOilPriceAdp.notifyDataSetChanged();
                }
            } else if (TextUtils.equals(tag, ConstantTag.HOME_OIL_FAIL)) {
                //油价获取失败
                delazyOilPriceCache();
            } else if (TextUtils.equals(tag, ConstantTag.GET_ORDER_SUCCESS)) {
                //获取订单总额成功
                PointModel model = (PointModel) t;
                String money = Currency.returnDollar(Currency.RMB, model.money, 0).replace("元", "");
                mTxtTotalOrder.setText(StringUtils.removePoint(money) + "元");
            } else if (TextUtils.equals(tag, ConstantTag.GET_ORDER_FAIL)) {
                //获取订单总额失败
                mTxtTotalOrder.setText("- -");
            } else if (TextUtils.equals(tag, ConstantTag.GET_POINT_SUCCESS)) {
                //获取总积分成功
                PointModel model = (PointModel) t;
                mTxtTotalPoint.setText(Currency.returnDollar(Currency.RMB, model.score, 1));
            } else if (TextUtils.equals(tag, ConstantTag.GET_POINT_FAIL)) {
                //获取总积分失败
                mTxtTotalPoint.setText("- -");
            }
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_ARTICLE_LIST)) {
                //获取今日油讯
                mSaveName.clear();
                mSaveUrl.clear();
                List<ArticleModel> mArticles = (List<ArticleModel>) t;
                if (null != mArticles && !mArticles.isEmpty()) {
                    mLinearArticle.setVisibility(View.VISIBLE);
                    for (ArticleModel model : mArticles) {
                        mSaveName.add(model.articleTitle);
                        mSaveUrl.add(model.articleUrl);
                    }
                    mTxtDayNews.startWithList(mSaveName);
                }
            } else if (TextUtils.equals(tag, ConstantTag.GET_ARTICLE_FAIL)) {
                //获取今日油讯失败
                delazyArticlesCache();
            }
        }
    }

    /**
     * 处理banner显示
     */
    public void showBannerPager(List<BannerModel> mBanners) {
        mBannerAdp = new BannerPagerAdapter(mBanners, frg);
        mViewpager.setAdapter(mBannerAdp);
        mIndicator.setViewPager(mViewpager);
    }

    /**
     * 处理banner数据返回
     */
    @Override
    public void handlerByPage(int flag1, int flag2) {
        switch (flag2) {
            case CodeStatus.Gegeneral.ERROR_NET:
//                List<BannerModel> mBanner = new ArrayList<BannerModel>();
//                mBanner.add(new BannerModel("", ""));
//                showBannerPager(mBanner);
                if (mSaveBanners.isEmpty()) {
                    delazyBannerCache();
                }
                break;
            //banner 数据加载失败
            case CodeStatus.Gegeneral.ERROR_DATA:
                if (mSaveBanners.isEmpty()) {
                    delazyBannerCache();
                }
//                if (mRecordBannerCount < 3) {
//                    mRecordBannerCount++;
//                    ((HomePresenter) presenter).getBanner();
//                } else {
//                    List<BannerModel> banners = ((HomePresenter) presenter).getCacheData();
//                    if (null != banners && !banners.isEmpty()) {
//                    } else {
//                        List<BannerModel> mBanner1 = new ArrayList<BannerModel>();
//                        mBanner1.add(new BannerModel("", ""));
//                        showBannerPager(mBanner1);
//                    }
//                }
                break;
            //banner 数据加载成功
            case CodeStatus.Gegeneral.DATA_SUCCESS_FULL:
                mRecordBannerCount = 0;
                break;
            case CodeStatus.Gegeneral.DATA_SUCCESS_NULL:
//                mRecordBannerCount = 0;
//                List<BannerModel> mBanner1 = new ArrayList<BannerModel>();
//                mBanner1.add(new BannerModel("", ""));
//                showBannerPager(mBanner1);
                mSaveBanners.clear();
                mLayoutBanner.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 热门活动点击事件
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (isLogin(true)) {
            PlayerModel item = (PlayerModel) mHotPlayAdp.getItem(position);
            //当前日期
            long currentTime = System.currentTimeMillis();
            if (currentTime >= item.getStartDate() && currentTime < item.getEndDate()) {
                String url = H5Address.GET_PRIZE + "custId=" + Session.getUserId() + "&lottery_id=" + item.getUid();
                CommH5WithTitleAty.startCommonH5(frg, url, "抽奖", false,false,"","","");
            } else if (currentTime < item.getStartDate()) {
                //活动未开始
                SVProgressHUD.showInfoWithStatus(frg, "该活动还未开始");
            } else {
                //活动已结束
                SVProgressHUD.showInfoWithStatus(frg, "该活动已结束");
            }
        }
    }

    /**
     * 栏目
     */
    public class OnRecyclerItemClick implements BaseAdapter.OnItemClickListener<HotExchangeModel, HomeChannelHolder> {
        @Override
        public void onItemClick(HomeChannelHolder holder, HotExchangeModel data) {
            int position = holder.getLayoutPosition();
            switch (position) {
                //优惠活动
                case 0:
                    showActivity(frg, FreePlayAty.class);
                    break;
                //油站导航
                case 1:
                    checkOilPermiss();
                    break;
                //违章查询
                case 2:
                    if (isLogin(true)) {
                        EntityCache<BreakRulesModel> mSaveCarsCache = new EntityCache<BreakRulesModel>(frg, BreakRulesModel.class, IntentKey.BreakCar.BREAK_CACHE);
                        List<BreakRulesModel> breaks = mSaveCarsCache.getListEntity(BreakRulesModel.class);
                        if (null != breaks && !breaks.isEmpty()) {
                            showActivity(frg, BreakRuleCarListAty.class);
                        } else {
                            showActivity(frg, BreakRulesCarAty.class);
                        }
                    }
                    break;
                //云记账
                case 3:
                    if (isLogin(true)) {
//                        if (Session.getUserBooks()) {
                        showActivity(frg, LookMyBillAty.class);
//                        } else {
//                            Intent intent = new Intent(frg, AddMyCloudBooks.class);
//                            startActivityForResult(intent, IntentCode.Book.BOOK_REQUEST_CODE);
//                        }
                    }
                    break;
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentCode.Book.BOOK_REQUEST_CODE) {
            if (resultCode == IntentCode.Book.BOOK_RESULT_CODE) {
//                showActivity(frg, LookMyBillAty.class);
            }
        }
    }

    /**
     * 热门兑换
     */
    public class OnRecyclerItemClick1 implements BaseAdapter.OnItemClickListener<HotExchangeModel, HotExchangeHolder> {
        @Override
        public void onItemClick(HotExchangeHolder holder, HotExchangeModel data) {
            int position = holder.getLayoutPosition();
            SVProgressHUD.showInfoWithStatus(frg, "点击" + position);
        }

    }

    /**
     * 检测用户定位权限
     */
    private void checkOilPermiss() {
        String[] perms = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        if (!mayRequestPermission(perms)) {
            return;
        }
        getBaiduLocation();
    }

    /**
     * 获取地址
     */
    public void getBaiduLocation() {
        showActivity(frg, BaiduMapAty.class);
    }

    @Override
    public void requestPermissionResult(boolean allowPermission) {
        if (allowPermission) {
            getBaiduLocation();
        }
    }
}
